﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Kovan.Model.Entity.UserCenter;
using Kovan.Model.Parameter.UserCenter;
using System.Linq.Expressions;
using Kovan.Model.ViewModel.UserCenter;

namespace Kovan.DAL.UserCenter
{
    public class DActModule : IDAL<act_module>
    {
        /// <summary>
        /// 获取列表数据
        /// </summary>
        /// <param name="applicationid"></param>
        /// <returns></returns>
        public List<act_module> GetList(int applicationid)
        {
            using (var db = new MysqlContext())
            {
                return db.act_module.Where(c => c.application_id.Value == applicationid).ToList();
            }
        }

        /// <summary>
        /// 获取列表数据
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public IEnumerable<act_module> GetList(PModule param)
        {
            using (var db = new MysqlContext())
            {
                var query = from m in db.act_module.AsQueryable().OrderByDescending(c => c.module_id.Value)
                            select m;

                if (param.module_id.HasValue)
                {
                    query = query.Where(o => o.module_id.Value == param.module_id.Value);
                }

                if (param.parent_id.HasValue)
                {
                    query = query.Where(o => o.parent_id.Value == param.parent_id.Value);
                }
                if (param.application_id.HasValue)
                {
                    query = query.Where(o => o.application_id.Value == param.application_id.Value);
                }
                if (param.enabled.HasValue)
                {
                    query = query.Where(o => o.enabled.Value == param.enabled.Value);
                }
                if (param.is_menu.HasValue)
                {
                    query = query.Where(o => o.is_menu.Value == param.is_menu.Value);
                }
                if (param.is_view.HasValue)
                {
                    query = query.Where(o => o.is_view.Value == param.is_view.Value);
                }
                if (string.IsNullOrEmpty(param.name) == false)
                {
                    query = query.Where(o => o.name.Equals(param.name));
                }
                if (string.IsNullOrEmpty(param.ukey) == false)
                {
                    query = query.Where(o => o.ukey.Equals(param.ukey));
                }
                //总条数
                param.TotalRows = query.Count();

                if (param.PageSize > 0 && param.PageIndex > 0)
                {
                    query.Skip(param.PageSize * (param.PageIndex - 1)).Take(param.PageSize);
                }

                return query.OrderByDescending(c => c.sort_order).ToList();
            }
        }

        /// <summary>
        /// 获取实体
        /// </summary>
        /// <param name="id">主键ID</param>
        /// <returns></returns>
        public act_module GetModel(long id)
        {
            using (var db = new MysqlContext())
            {
                return db.act_module.FirstOrDefault(c => c.module_id.Value == id);
            }
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="model">实体</param>
        /// <returns></returns>
        public int Add(act_module model)
        {
            using (var db = new MysqlContext())
            {
                db.act_module.Add(model);
                return db.SaveChanges();
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id">主键ID</param>
        /// <returns></returns>
        public int Delete(long id)
        {
            using (var db = new MysqlContext())
            {
                var model = db.act_module.FirstOrDefault(a => a.module_id == id);
                if (model != null)
                {
                    db.act_module.Remove(model);
                }
                return db.SaveChanges();
            }
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public int BatchDelete(IEnumerable<string> ids)
        {
            using (var db = new MysqlContext())
            {
                foreach (var id in ids)
                {
                    var moduleid = long.Parse(id);
                    var model = db.act_module.FirstOrDefault(a => a.module_id == moduleid);
                    if (model != null)
                    {
                        db.act_module.Remove(model);
                    }
                }
                return db.SaveChanges();
            }
        }

        //public int BatchDelete(List<long> ids)
        //{
        //    using (var db = new MysqlContext())
        //    {
        //        db.act_module.
        //        var model = db.act_module.Where(c => ids.Contains(c.module_id.Value));
        //        db.act_module.Remove(null);
        //        return db.SaveChanges();
        //    }
        //}

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="model">实体</param>
        /// <returns></returns>
        public int Update(act_module model)
        {
            using (var db = new MysqlContext())
            {
                db.Set<act_module>().Attach(model);
                db.Entry(model).State = System.Data.EntityState.Modified;
                return db.SaveChanges();

            }
        }

        public IEnumerable<EModuleAuthority> GetModuleAuthority(PModule param)
        {
            using (var db = new MysqlContext())
            {
                var query = from m in db.act_module
                           join ra in db.act_authority on m.module_id.Value equals ra.module_id.Value into g
                           //group m by m.module_id.Value into g
                           select new EModuleAuthority
                           {
                               module=m,
                               authoritys=g.Select(c=>c).OrderBy(c=>c.code_order.Value)
                           };
                           

                if (param.module_id.HasValue)
                {
                    query = query.Where(o => o.module.module_id.Value == param.module_id.Value);
                }

                if (param.parent_id.HasValue)
                {
                    query = query.Where(o => o.module.parent_id.Value == param.parent_id.Value);
                }
                if (param.application_id.HasValue)
                {
                    query = query.Where(o => o.module.application_id.Value == param.application_id.Value);
                }
                if (param.enabled.HasValue)
                {
                    query = query.Where(o => o.module.enabled.Value == param.enabled.Value);
                }
                if (param.is_menu.HasValue)
                {
                    query = query.Where(o => o.module.is_menu.Value == param.is_menu.Value);
                }
                if (param.is_view.HasValue)
                {
                    query = query.Where(o => o.module.is_view.Value == param.is_view.Value);
                }
                if (string.IsNullOrEmpty(param.name) == false)
                {
                    query = query.Where(o => o.module.name.Equals(param.name));
                }
                if (string.IsNullOrEmpty(param.ukey) == false)
                {
                    query = query.Where(o => o.module.ukey.Equals(param.ukey));
                }
                //总条数
                param.TotalRows = query.Count();

                if (param.PageSize > 0 && param.PageIndex > 0)
                {
                    query.Skip(param.PageSize * (param.PageIndex - 1)).Take(param.PageSize);
                }

                return query.OrderByDescending(c => c.module.sort_order).ToList();
            }
        }
    }
}
